/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package wifipositioner;

import org.pi4.locutil.MACAddress;
import org.pi4.locutil.trace.TraceEntry;

/**
 *
 * @author hh
 */
public class DistanceCalculator {
        
    
    public static double calcDistance(TraceEntry offline, TraceEntry online, int numberOfSamples) 
    {        
        int i = 0; 

        double dist = 0;

        for(MACAddress mOnline : online.getSignalStrengthSamples().getSortedAccessPoints()) {

            if(i > numberOfSamples)
                break;

            double signalStrengthOffline = 0;
            double signalStrengthOnline = 0;

            if(offline.getSignalStrengthSamples().containsKey(mOnline) == false) 
            {
                dist = Double.MAX_VALUE;
                break;
            }

            signalStrengthOffline = offline.getSignalStrengthSamples().getAverageSignalStrength(mOnline);
            signalStrengthOnline = online.getSignalStrengthSamples().getAverageSignalStrength(mOnline);

            //System.out.println("Dist " + dist);
            dist += Math.pow(signalStrengthOnline - signalStrengthOffline, 2);
            i++;
        }

        dist = Math.sqrt(dist);

        return dist;
    }
    
//     public static double calcDistance(TraceEntry offline, TraceEntry online, int numberOfSamples) 
//    {        
//        int i = 0;
//        double dist = 0;
//
//        for(MACAddress mOnline : online.getSignalStrengthSamples().getSortedAccessPoints()) 
//        {
//            if(i > numberOfSamples)
//                break;
//
//            if(offline.getSignalStrengthSamples().containsKey(mOnline))
//            {
//                double signalStrengthOffline = 0;
//                double signalStrengthOnline = 0;
//
//                signalStrengthOffline = offline.getSignalStrengthSamples().getAverageSignalStrength(mOnline);
//                signalStrengthOnline = online.getSignalStrengthSamples().getAverageSignalStrength(mOnline);
//
//                dist += Math.pow(signalStrengthOnline - signalStrengthOffline, 2);
//                i++;
//            }
//        }
//
//        if(i == 0)
//            dist = Double.MAX_VALUE;
//        
//        dist = Math.sqrt(dist);
//
//        return dist;
//    }
   
    
    public static double calcDistance(ModelBasedTrace offline, TraceEntry online, int numberOfSamples) 
    {        
        int i = 0;
        double dist = 0;

        for(MACAddress mOnline : online.getSignalStrengthSamples().getSortedAccessPoints()) 
        {
            if(i > numberOfSamples)
                break;

            if(offline.getSignalStrengthSamples().containsKey(mOnline))
            {
                double signalStrengthOffline = 0;
                double signalStrengthOnline = 0;

                signalStrengthOffline = offline.getSignalStrengthSamples().get(mOnline);
                signalStrengthOnline = online.getSignalStrengthSamples().getAverageSignalStrength(mOnline);

                dist += Math.pow(signalStrengthOnline - signalStrengthOffline, 2);
                i++;
            }
        }

        if(i == 0)
            dist = Double.MAX_VALUE;
        
        dist = Math.sqrt(dist);

        return dist;
    }
    
}
